
package com.compuvest.web;

import java.io.Serializable;
import java.util.ArrayList;

public class Items implements Serializable {

    public final static int SPECIAL =       0x1;    // 0000 0001
    public final static int CLOSEOUT =      0x1<<1; // 0000 0010
    public final static int COMMINGSOON =   0x1<<2; // 0000 0100 (Ships in 3-5 days)
    public final static int BESTPRICE =     0x1<<3; // 0000 1000

    public static final int LINE_ON_PAGE =  15;

    public final static String PRICEFORM = "<nobr>$%,.2f</nobr>";

    private ArrayList<Item> contents;
    private ArrayList<Category> categories;
    private ArrayList<Category> subcategories;
    private ArrayList<Preference> prefs;
    private int eventTime;
    private int itemsCount;
    private int categoryID;
    private int subcategoryID;
    private String categoryName;
    private String subcategoryName;
    private boolean hotdeals;

    public Items() {
	contents = new ArrayList<Item>(LINE_ON_PAGE);
	categories = new ArrayList<Category>(50);
	subcategories = new ArrayList<Category>(50);
	prefs = new ArrayList<Preference>(7);
    }

    public String getEventTime()                    { return "<font color=#eaf2ea>" + eventTime + "</font>"; }
    public void setEventTime(int eventTime)         { this.eventTime = eventTime; }

    public ArrayList<Item> getItems()               { return contents; }
    public void addItem(Item item)                  { contents.add(item); }

    public int getCategoryID()                      { return categoryID; }
    public void setCategoryID(int categoryID)       { this.categoryID = categoryID; }

    public int getSubcategoryID()                   { return subcategoryID; }
    public void setSubcategoryID(int categoryID)    { this.subcategoryID = categoryID; }


    public String getCategoryName()                 { return categoryName; }
    public void setCategoryName(String name)        { this.categoryName = name; }

    public String getSubcategoryName()              { return subcategoryName; }
    public void setSubcategoryName(String name)     { this.subcategoryName = name; }


    public ArrayList<Category> getCategories()      { return categories; }
    public void addCategory(Category category)      { categories.add(category); }

    public ArrayList<Category> getSubcategories()   { return subcategories; }
    public void addSubcategory(Category category)   { subcategories.add(category); }

    // for itemspage select search
    public boolean haveHotDeals()                   { return hotdeals; }
    public void setHotDeals()                       { this.hotdeals = true; }

    public ArrayList<Preference> getPreference()    { return prefs; }
    public void addPreference(Preference pref)      { this.prefs.add(pref); }

    public int getItemsCount()                      { return itemsCount; }
    public void setItemsCount(int itemsCount)       { this.itemsCount = itemsCount; }

    public int size()                               { return contents.size(); }

    public int getPages() {
        return (itemsCount % LINE_ON_PAGE == 0) ? itemsCount / LINE_ON_PAGE : itemsCount / LINE_ON_PAGE + 1;
    }

    public String getPriceMessage(int mask, boolean message) {
	if((mask & BESTPRICE) != 0) {
            return("Best Price");
        }
	else if((mask & SPECIAL) != 0) {
            return("Special");
        }
	else if((mask & CLOSEOUT) != 0) {
            return("Clearance");
        }

        return message ? " Price" : "&nbsp;";
    }


    // for Reseller
    public String getResellerPriceLine(final Item item) {

	StringBuilder line = new StringBuilder(150);
        line.append("<div class=rsp>&#x95; Your price: ");

        if(item.getResPrice1() != 0d) {

            if(item.getResPrice1() != 0d) {
                line.append("1+ ").append(String.format(PRICEFORM, item.getResPrice1()));
            }
            if(item.getResQuantity2() != 0 && item.getResPrice2() != 0d) {
                line.append("&nbsp; | &nbsp;").append(item.getResQuantity2() + 1).append("+ ").append(String.format(PRICEFORM, item.getResPrice2()));
            }
            if(item.getResQuantity3() != 0 && item.getResPrice3() != 0d) {
                line.append("&nbsp; | &nbsp;").append(item.getResQuantity3() + 1).append("+ ").append(String.format(PRICEFORM, item.getResPrice3()));
            }
        }
        else {
            line.append("1+ ").append(String.format(PRICEFORM, item.getPrice()));
        }
        line.append("</div>");

	return line.toString();
    }
}
